﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using Puzzler.Plugin.Interface;
using Puzzler.Plugin.Interface.Attributes;

namespace Plugin.Default.SwedenGrid
{
    [PuzzleInputHandler]
    public sealed class SwedenGridInputHandler : GridPuzzleInputHandler<SwedenGridModel,SwedenGridCellInfo>
    {
        public SwedenGridInputHandler(SwedenGridModel model) : base(model)
        {
        }

        protected override void OnGridCellDoubleClick(int x, int y, SwedenGridCellInfo cellInfo)
        {
            SwedenGridCellInfo info = SwedenGridCellInfoWindow.ShowDialog(cellInfo);

            if (info != null)
            {
                cellInfo.CellType = info.CellType;
                cellInfo.CellContent = info.CellContent;
                cellInfo.AdditionalCellContent = info.AdditionalCellContent;
                cellInfo.ShowAlways = info.ShowAlways;
            }
        }

        protected override void OnKeyPressed(SwedenGridCellInfo cellInfo, Key key)
        {
            if (cellInfo == null)
                return;
            if ((key < Key.A || key > Key.Z) && key != Key.Delete)
                return;

            if (cellInfo.CellType == SwedenGridCellType.Question)
                if (MessageBox.Show("You trying to overwrite a question box! You are sure?", "Overwrite Question-Box", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.No)
                    return;

            if (key == Key.Delete)
            {
                cellInfo.CellType = SwedenGridCellType.Character;
                cellInfo.AdditionalCellContent = null;
                cellInfo.CellContent.Text = "";
            }
            else
            {
                cellInfo.CellType = SwedenGridCellType.Character;
                cellInfo.AdditionalCellContent = null;
                cellInfo.CellContent.Text = key.ToString();
            }

            switch (Model.SelectionGoDirection)
            {
                case SelectionGoDirection.None:
                    break;
                case SelectionGoDirection.Right:
                    Model.GoSelectionRight();
                    break;
                case SelectionGoDirection.Left:
                    Model.GoSelectionLeft();
                    break;
                case SelectionGoDirection.Bottom:
                    Model.GoSelectionBottom();
                    break;
                case SelectionGoDirection.Top:
                    Model.GoSelectionTop();
                    break;
                default:
                    throw new NotImplementedException(Model.SelectionGoDirection.ToString());
            }
        }
    }
}
